﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ActivitiesControl.Domain.Services;
using ActivitiesControl.GUI.Web.Utils;


namespace ActivitiesControl.GUI.Web.Reports
{
	public partial class MonthActivities : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindFilters();
				ReportObjectDataSource.DataBind();
			}
		}

		protected void ReportButton_Click(object sender, EventArgs e)
		{
			ActvitiesReportViewer.LocalReport.Refresh();
		}

		void BindFilters()
		{
			IManageActivityService service = ServicesProvider.GetManageActivityService();
			IUserService u = ServicesProvider.GetUserServices();

			var user = u.GetUserByLogin(LoggedUser.RequesterUser);

			CustomerDropDownList.DataSource = service.GetCustomersForForm(user.Id);
			CustomerDropDownList.DataBind();

			int monthsToShow = 6;

			DateTime startDate = DateTime.Now.AddMonths(monthsToShow * -1);

			DateDropDownList.Items.Clear();

			for (int i = 1; i <= monthsToShow; i++)
			{
				string item = startDate.AddMonths(i).ToString("yyyy/MM");

				DateDropDownList.Items.Add(item);

				if (startDate.AddMonths(i).Equals(DateTime.Now.AddMonths(-1)))
					DateDropDownList.Items.FindByValue(item).Selected = true;
			}
		}

		protected void ReportObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["customerId"] = Convert.ToInt32(CustomerDropDownList.SelectedValue);
			e.InputParameters["month"] = Convert.ToDateTime(DateDropDownList.SelectedValue).ToString("MM");
			e.InputParameters["year"] = Convert.ToDateTime(DateDropDownList.SelectedValue).ToString("yyyy");
		}
	}
}